package com.wanchuang.qhbb.module.start;

/**
 * 文件名 : StartActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/7 14:06
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;


import com.wanchuang.qhbb.BuildConfig;
import com.wanchuang.qhbb.MainActivity;
import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.base.BaseActivity;
import com.wanchuang.qhbb.base.MainApplication;
import com.wanchuang.qhbb.help.UserHelper;
import com.wanchuang.qhbb.module.login.LoginActivity;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.utils.PreferenceSettings;
import com.google.protobuf.InvalidProtocolBufferException;


public class StartActivity extends BaseActivity {

    private String IS_FIRST = "is_have_welcome";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        boolean isFirst = PreferenceSettings.getBoolean(IS_FIRST, true);
        MainApplication.setServiceHost(BuildConfig.SERVERHEAD);
        NetworkManager.INSTANCE.post(Apis.get_site_domain, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
//                MainApplication.setServiceHost(Index.get_site_domain.parseFrom(response).getHost());
                MainApplication.setServiceHost(BuildConfig.SERVERHEAD);
                new Handler().postDelayed(() -> {
                    if (isFirst) {
                        startActivity(GuideActivity.class);
                    } else {
                        if (UserHelper.isLogin() && UserHelper.getIsRenZhen() == 1 && UserHelper.getIsBank() == 1) {
                            Intent intent = new Intent(StartActivity.this, MainActivity.class);
                            if (getIntent().getStringExtra("EXTRA_BUNDLE") != null) {
                                intent.putExtra("EXTRA_BUNDLE",
                                        getIntent().getStringArrayExtra("EXTRA_BUNDLE"));
                            }
                            startActivity(intent);
                        } else {
                            UserHelper.loginOut();
                            startActivity(LoginActivity.class);
                        }
                    }
                    finish();
                }, 1000);
            }

            @Override
            public void onError(int code, String errorMessage) {
                showHintDialog(errorMessage);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDestory = true;
    }
}
